<?php
/**
 * User list administer
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo
 */
class User_AppController extends Zend_Controller_Action
{
    /**
     * Initialisation
     *
     * @return void
     */
    public function init()
    {
    }
    /**
     * Show user list
     *
     * @return void
     */
    public function indexAction()
    {
        try {
            $this->setCss('window');
            $this->setCss('user');

            $layout = Zend_Layout::getMvcInstance();
            $layout->appTab = User_Model_Tab::render($this->view);

            if($this->_request->isPost()) {
                $this->_forward('delete');
            }
            $database = new User_Model_Database;
            if(($users = $database->getUser()) === FALSE) {

                $this->_forward(
                    'app',
                    'error',
                    'error',
                    array(
                        'message' => 'Benutzer konnten nicht gelesen werden!',
                        'back' => array('module' => 'rockitbos')));
            } else {
                $form = new User_Form_List;
                $this->view->form = $form->build($users);
            }
        } catch (Exception $e) {

            $this->_forward('app', 'error', 'error', array('log' => $e));
        }
    }
    /**
     * User delete
     *
     * @throws Exception
     * @return void
     */
    public function deleteAction() {
        try {
            $userId = (integer) $this->_request->getPost('userlist');

            if(empty($userId)) {
                $this->_forward(
                    'app',
                    'error',
                    'error',
                    array(
                        'message' => 'Bitte einen Benutzer auswählt!',
                        'back' => array('module' => 'rockitbos', 'app' => 'user')));

            } else {
                $database = new User_Model_Database;
                if($database->delete($userId) === FALSE) {
                    
                    $this->_forward(
                        'app',
                        'error',
                        'error',
                        array(
                            'message' => 'Benutzer konnte nicht gelöscht werden!',
                            'back' => array('module' => 'rockitbos', 'app' => 'user')));
                }
            }
        } catch (Exception $e) {

            $this->_forward('app', 'error', 'error', array('log' => $e));
        }
    }

    /**
     * Set css file
     *
     * @return void
     */
    protected function setCss($file) {

        $this->view
             ->headLink()
             ->appendStylesheet(
                 DIRECTORY_SEPARATOR . 'user' .
                 DIRECTORY_SEPARATOR . 'css' .
                 DIRECTORY_SEPARATOR . $file . ".css"
               );
    }
}